<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class ta extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_tahunajar');
        // load library
        $this->load->library('tnotification');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/ta/index.html");
        // data
        $result = $this->m_tahunajar->get_detail_data_tahunajar();
        $this->smarty->assign('result', $result);
        $set = !empty($result) ? 'y' : 'n';
        $this->smarty->assign('set', $set);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // halaman tambah
    public function add() {
        // set page rules
        $this->_set_page_rule("C");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/ta/add.html");
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah
    public function add_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('tahun', 'Tahun', 'trim|required|maxlength[4]|numeric');
        $this->tnotification->set_rules('semester', 'Semester', 'trim|required|maxlength[1]|numeric');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $tahun = $this->input->post('tahun');
            $semester = $this->input->post('semester');
            //--
            $params = array($semester, $tahun);
            // delete data
            $this->m_tahunajar->delete_data_tahunajar();
            // input data
            if ($this->m_tahunajar->insert_data_tahunajar($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                // redirect
                redirect('pengaturan/ta');
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('pengaturan/ta/add');
    }

    // update data
    public function edit() {
        // set page rules
        $this->_set_page_rule("U");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/ta/edit.html");
        // data
        $result = $this->m_tahunajar->get_detail_data_tahunajar();
        $this->smarty->assign('result', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses update
    public function edit_process() {
        // set page rules
        $this->_set_page_rule("U");
        // cek input
        $this->tnotification->set_rules('tahun', 'Tahun', 'trim|required|maxlength[4]|numeric');
        $this->tnotification->set_rules('semester', 'Semester', 'trim|required|maxlength[1]|numeric');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $tahun = $this->input->post('tahun');
            $semester = $this->input->post('semester');
            //--
            $params = array($semester, $tahun);
            // delete data
            $this->m_tahunajar->delete_data_tahunajar();
            // input data
            if ($this->m_tahunajar->insert_data_tahunajar($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                // redirect
                redirect('pengaturan/ta');
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('pengaturan/ta/edit');
    }

}